/*
 * 作用：用户相关状态存储
 * /

// 状态即是数据
var state = {
    IsRefresh: true, // 判断页面是否刷新
    isLogin: window.sessionStorage.getItem('isLogin') ? Boolean(window.sessionStorage.getItem('isLogin')) : false,
    menuInfo: (window.sessionStorage.getItem('menuInfo')) ? JSON.parse(window.sessionStorage.getItem('menuInfo')) : [],
    userInfo: (window.sessionStorage.getItem('userInfo')) ? JSON.parse(window.sessionStorage.getItem('userInfo')) : []
}

// 定义getters 读取状态
var getters = {
    getRefresh(state) {
        return state.IsRefresh
    },
    getIsLogin(state) {
        return state.isLogin
    },
    getMenuInfo(state) {
        return [...state.menuInfo]
    },
    getUserInfo(state) {
        return [...state.userInfo]
    }
}

// 定义action,要执行的操作，如流程判断，异步请求等
var actions = {
    setNoRefresh({ commit }) {
        commit('setNoRefresh')
    },
    loginSuccess({ commit }, data) {
        commit('LoginSuccess', data)
    },
    loginEdit({ commit }) {
        commit('LoginEdit')
    }
}

// 处理状态的改变
var mutations = {
    setNoRefresh(state) {
        state.IsRefresh = false // 设置不刷新
    },
    LoginSuccess(state, data) { // 登录成功
        state.isLogin = true
        state.menuInfo = data.menuInfo
        state.userInfo = data.userInfo
        window.sessionStorage.setItem('isLogin', true)
        window.sessionStorage.setItem('menuInfo', JSON.stringify(data.menuInfo))
        window.sessionStorage.setItem('userInfo', JSON.stringify(data.userInfo))
    },
    loginEdit(state) { // 注销
        state.isLogin = false
        state.roleData = []
        state.userInfo = []
        window.sessionStorage.setItem('isLogin', false)
        window.sessionStorage.removeItem('roleData')
        window.sessionStorage.removeItem('userInfo')
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
